package com.jadefashion.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.Setmeal;
import com.jadefashion.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
//    @Reference(mock = "com.jadefashion.service.SetmealServiceMock",timeout = 1200)
    @Reference
    private SetmealService setmealService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("findAllSetmeal.do")
    public Object findAllSetmeal(){
        Result result= null;
        try {

            List<Setmeal> list1=setmealService.findAll();
            if(list1==null){
                return result = new Result(false,"当前页面火爆，请稍后再试");
            }
            result = new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS,list1);

//            redisTemplate.delete("setmealList");
            //setmealList
            List<String> list = redisTemplate.opsForList().range("setmealList", 0, -1);
            if(list!=null&&!list.isEmpty()){//缓存中有值 将缓存中数据返回前台
               return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
            }else {//缓存中没有值，从数据库查
                List<Setmeal> setmealList=setmealService.findAll();
                if (setmealList!=null){
                    for (Setmeal setmeal : setmealList) {
                        String s = JSON.toJSONString(setmeal);
                        redisTemplate.opsForList().leftPush("setmealList",s);//将每个setmeal对象转化字符串然后添加到缓存
                        list.add(s);
                    }
                }
                return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("抛出异常");
            result = new Result(false, MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
        return result;
    }

    @RequestMapping("findSetMealDetail.do")
    public Object findSetMealDetail(@RequestParam("id") Integer id){
        Result result= null;
        try {
//            redisTemplate.delete("setmeal");
//            查询缓存中所有数据
            List<String> list = redisTemplate.opsForList().range(id+"", 0, -1);
            if (list!= null && !list.isEmpty()){//缓存中有值 将缓存中数据返回前台
                return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
            }else {
                Setmeal setmeal=setmealService.findSetMealDetail(id);
                String s = JSONArray.toJSON(setmeal).toString();
                redisTemplate.opsForList().leftPush(id+"",s);//将每个setmeal对象转化字符串然后添加到缓存
                list.add(s);
                result = new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS,list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(true, MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
        return result;
    }

    @RequestMapping("/findSetmealByid.do")
    public Result findSetmealByid(@RequestParam("id") Integer id){
        Result result= null;
        try {
            Setmeal setmeal = setmealService.findPreSetmealByid(id);
            result = new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            result =  new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
        return result;
    }
}
